/**
 * DictionaryItemDaoImpl.java
 * Created at 2017-07-06
 * Created by Jackie liu
 * Copyright (C) 2014, All rights reserved.
 */
package com.huabo.dao.impl.oracle;

import com.huabo.base.BaseDaoImpl;
import com.huabo.dao.inter.DictionaryItemDao;
import com.huabo.model.DictionaryItem;
import com.huabo.sql.builder.WhereBuilder;
import com.huabo.utils.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;

/**
 * <p>ClassName: DictionaryItemDaoImpl</p>
 * <p>Description: 字典项管理Dao实现</p>
 * <p>Author: Jackie liu</p>
 * <p>Date: 2017-07-06</p>
 */
@Repository
public class DictionaryItemDaoImpl extends BaseDaoImpl<DictionaryItem, Long> implements DictionaryItemDao {

	@Override
	public Page<DictionaryItem> queryPage(Map<String, Object> params) {
		WhereBuilder qc = new WhereBuilder();
		qc.setOrderBy("F_VALUE DESC");
		if (params != null) {
			initPageParam(qc, params);
			String searchInput = (String) params.get("searchInput");
			String dicId = (String) params.get("dicId");
			qc.andEquals("F_DICTIONARY_ID", dicId);
			if (StringUtils.isNotEmpty(searchInput)) {
				qc.andFullLike("F_NAME", searchInput);
					
			}
		}
		return queryPage(qc);
	}
	
	@Override
	public int removeByDicId(final Long[] dicIds) {
		String sql=String.format("DELETE FROM %s WHERE F_DICTIONARY_ID = ?", domainModelAnalysis.getTableName());
		int[] batchUpdate = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

			@Override
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setObject(1, dicIds[i]);
			}

			@Override
			public int getBatchSize() {
				return dicIds.length;
			}
		});
		return batchUpdate.length;
	}
}